R语言 矩阵 您所在的位置:网站首页 r语言 建立空矩阵 R语言 矩阵

R语言 矩阵

2024-03-06 02:57| 来源: 网络整理| 查看: 265

R语言 矩阵

矩阵 是数字在行和列中的一种矩形排列。在一个矩阵中,我们知道行是水平方向的,列是垂直方向的。在R编程中,矩阵是二维的、同质的数据结构。这些是一些矩阵的例子:

R - 矩阵

创建一个矩阵

要在R语言中创建一个矩阵,你需要使用名为 matrix() 的函数 。 这个 matrix() 的参数是向量中元素的集合。你必须传递你希望在你的矩阵中拥有多少行和多少列。

注意: 默认情况下,矩阵是按列顺序排列的。

# R program to create a matrix    A = matrix(         # Taking sequence of elements    c(1, 2, 3, 4, 5, 6, 7, 8, 9),       # No of rows   nrow = 3,         # No of columns   ncol = 3,               # By default matrices are in column-wise order   # So this parameter decides how to arrange the matrix   byrow = TRUE          )    # Naming rows rownames(A) = c("a", "b", "c")    # Naming columns colnames(A) = c("c", "d", "e")    cat("The 3x3 matrix:\n") print(A)

输出:

The 3x3 matrix: c d e a 1 2 3 b 4 5 6 c 7 8 9 创建特殊的矩阵

R允许通过使用传递给matrix()函数的参数来创建各种不同类型的矩阵。

所有行和列都由一个常数’k ‘ 填充的矩阵 :

要创建这样一个矩阵,语法如下:

语法: matrix(k, m, n)

参数:

k: 常数

m: 行的数量

n: 列的数量

示例: # R program to illustrate # special matrices   # Matrix having 3 rows and 3 columns # filled by a single constant 5 print(matrix(5, 3, 3)) 输出: [,1] [,2] [,3] [1,] 5 5 5 [2,] 5 5 5 [3,] 5 5 5 对角线矩阵:

对角线矩阵是一个矩阵,其中主对角线以外的条目都是零。要创建这样一个矩阵,语法如下:

语法: diag(k, m, n)

参数:

k: 常数/数组

m: 行的数量

n: 列的数量

示例: # R program to illustrate # special matrices   # Diagonal matrix having 3 rows and 3 columns # filled by array of elements (5, 3, 3) print(diag(c(5, 3, 3), 3, 3)) 输出: [,1] [,2] [,3] [1,] 5 0 0 [2,] 0 3 0 [3,] 0 0 3 单位矩阵:

一个正方形矩阵,其中主对角线上的所有元素都是1,其他元素都是0。要创建这样的矩阵,语法如下:

语法: diag(k, m, n)

参数:

k: 1

m: 行的数量

n: 列的数量

示例: # R program to illustrate # special matrices   # Identity matrix having # 3 rows and 3 columns print(diag(1, 3, 3)) 输出: [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 矩阵度量

矩阵度量是指一旦创建了一个矩阵,那么

你如何知道矩阵的维度? 你怎么能知道矩阵中有多少行? 矩阵中有多少列? 矩阵中有多少个元素? 是我们通常想要回答的问题。

例如:

# R program to illustrate # matrix metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   cat("Dimension of the matrix:\n") print(dim(A))   cat("Number of rows:\n") print(nrow(A))   cat("Number of columns:\n") print(ncol(A))   cat("Number of elements:\n") print(length(A)) # OR print(prod(dim(A)))

输出:

The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Dimension of the matrix: [1] 3 3 Number of rows: [1] 3 Number of columns: [1] 3 Number of elements: [1] 9 [1] 9 访问矩阵中的元素

我们可以使用与数据框架相同的约定来访问矩阵中的元素。所以,你会有一个矩阵,后面是一个方括号,在数组之间有一个逗号。逗号之前的值用于访问行,逗号之后的值用于访问列。让我们通过一个简单的R代码来说明这个问题。

访问行:

# R program to illustrate # access rows in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   # Accessing first and second row cat("Accessing first and second row\n") print(A[1:2, ])

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 Accessing first and second row [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6

访问列:

# R program to illustrate # access columns in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   # Accessing first and second column cat("Accessing first and second column\n") print(A[, 1:2])

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 Accessing first and second column [, 1] [, 2] [1, ] 1 2 [2, ] 4 5 [3, ] 7 8

访问矩阵的元素:

# R program to illustrate # access an entry in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   # Accessing 2 print(A[1, 2])   # Accessing 6 print(A[2, 3])

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 [1] 2 [1] 6

访问子矩阵:

我们可以使用 冒号(:) 操作符访问矩阵中的子矩阵。

# R program to illustrate # access submatrices in a matrix   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   cat("Accessing the first three rows and the first two columns\n") print(A[1:3, 1:2])

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 Accessing the first three rows and the first two columns [, 1] [, 2] [1, ] 1 2 [2, ] 4 5 [3, ] 7 8 修改矩阵的元素

在R中,你可以通过直接赋值来修改矩阵的元素。

例子:

# R program to illustrate # editing elements in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   # Editing the 3rd rows and 3rd column element # from 9 to 30 # by direct assignments A[3, 3] = 30   cat("After edited the matrix\n") print(A)

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 After edited the matrix [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 30 矩阵连接

矩阵连接是指合并现有矩阵的行或列。

行的连接: 行与矩阵的连接是用 rbind() 完成的 。

# R program to illustrate # concatenation of a row in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   # Creating another 1x3 matrix B = matrix(   c(10, 11, 12),   nrow = 1,   ncol = 3 ) cat("The 1x3 matrix:\n") print(B)   # Add a new row using rbind() C = rbind(A, B)   cat("After concatenation of a row:\n") print(C)

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 The 1x3 matrix: [, 1] [, 2] [, 3] [1, ] 10 11 12 After concatenation of a row: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 [4, ] 10 11 12

列的连接: 列与矩阵的连接是通过 cbind() 完成的 。

# R program to illustrate # concatenation of a column in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   # Creating another 3x1 matrix B = matrix(   c(10, 11, 12),   nrow = 3,   ncol = 1,   byrow = TRUE ) cat("The 3x1 matrix:\n") print(B)   # Add a new column using cbind() C = cbind(A, B)   cat("After concatenation of a column:\n") print(C)

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 The 3x1 matrix: [, 1] [1, ] 10 [2, ] 11 [3, ] 12 After concatenation of a column: [, 1] [, 2] [, 3] [, 4] [1, ] 1 2 3 10 [2, ] 4 5 6 11 [3, ] 7 8 9 12

尺寸不一致: 注意,在做这个矩阵连接之前,你必须确保矩阵之间尺寸的一致性。

# R program to illustrate # Dimension inconsistency in metrics concatenation   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("The 3x3 matrix:\n") print(A)   # Creating another 1x3 matrix B = matrix(   c(10, 11, 12),   nrow = 1,   ncol = 3, ) cat("The 1x3 matrix:\n") print(B)   # This will give an error # because of dimension inconsistency C = cbind(A, B)   cat("After concatenation of a column:\n") print(C)

输出:

The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 The 1x3 matrix: [, 1] [, 2] [, 3] [1, ] 10 11 12 Error in cbind(A, B) : number of rows of matrices must match (see arg 2) 删除矩阵的行和列

要删除一行或一列,首先需要访问该行或列,然后在该行或列前插入一个负号。它表示你必须删除该行或列。

行的删除:

# R program to illustrate # row deletion in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("Before deleting the 2nd row\n") print(A)   # 2nd-row deletion A = A[-2, ]   cat("After deleted the 2nd row\n") print(A)

输出:

Before deleting the 2nd row [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 After deleted the 2nd row [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 7 8 9

列的删除:

# R program to illustrate # column deletion in metrics   # Create a 3x3 matrix A = matrix(   c(1, 2, 3, 4, 5, 6, 7, 8, 9),   nrow = 3,               ncol = 3,               byrow = TRUE          ) cat("Before deleting the 2nd column\n") print(A)   # 2nd-row deletion A = A[, -2]   cat("After deleted the 2nd column\n") print(A)

输出:

Before deleting the 2nd column [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 After deleted the 2nd column [, 1] [, 2] [1, ] 1 3 [2, ] 4 6 [3, ] 7 9


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有